//! \file MenuMain.cpp
//! \brief TODO: Document
// -----------------------------------------------------------------------------
//! This program is free software; you can redistribute it and/or
//! modify it under the terms of the GNU General Public License
//! as published by the Free Software Foundation; either version 2
//! of the License, or (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program; if not, write to the Free Software Foundation,
//! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------------

#include "Elements.h"
#include "GuiCEGUI.h"
#include <stddef.h>

using namespace CEGUI;

// =====[ Local Definitions ]===================================================

// =====[ Local DataTypes ]=====================================================

// =====[ Local Variables ]=====================================================

// =====[ Local Functions ]=====================================================

// =====[ Private (Internal) Methods ]==========================================

// =====[ Local Implementation ]================================================


// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
tERROR MenuMain::create(Window *pParent)
{
  Window *pMenuBar;

  // If no parent was provided, we will attach it to the root.
  if (pParent == NULL) { pParent = GuiCEGUI::getRoot(); }

  // Create the menu bar.
  pMenuBar = GuiCEGUI::getWm()->createWindow("TaharezLook/Menubar");             // Create the menu.
  if (pMenuBar == NULL) { /* TODO: error */ }

  // Configure the menu bar as needed.
  UDim MenuBarBottom(0, GuiCEGUI::getFont()->getLineSpacing(2));
  pMenuBar->setArea(UDim(0,0), UDim(0,0), UDim(1,0), MenuBarBottom); // Set the size.
  pMenuBar->setAlwaysOnTop(true);                                    // Keep it on top.
  pParent->addChildWindow(pMenuBar);                                 // Attach it to the parent.

  // Add the menu items to the menu bar.
  addMenuItemFile(pMenuBar); // [File]
  addMenuItemHelp(pMenuBar); // [Help]

  // Return success.
  return (ERR_NONE);
}


// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
tERROR MenuMain::addMenuItemFile(Window *pParent)
{
  WindowManager *pWm;
  Window *pMenu, *pPopup, *pItem;

  // Validate our arguments.
  if (pParent == NULL) { /* TODO: error */ }

  // Get the window manager to work with.
  pWm = GuiCEGUI::getWm();

  // Create the menu item.
  pMenu = pWm->createWindow("TaharezLook/MenuItem");
  pMenu->setText("File");         // Set the displayed text.
  pParent->addChildWindow(pMenu); // Attach to the parent.

  // Create the menu popup.
  pPopup = pWm->createWindow("TaharezLook/PopupMenu");
  pMenu->addChildWindow(pPopup);  // Attach the popup to the menu.

  // "Quit" item in the menu.
  pItem = pWm->createWindow("TaharezLook/MenuItem");
  pItem->setText("Quit");         // Set the displayed text.
  //item->subscribeEvent("Clicked", Event::Subscriber(&ScrollablePaneSample::fileQuit, this));
  pItem->subscribeEvent("Clicked", Event::Subscriber(&eventFileQuit));
  pPopup->addChildWindow(pItem);  // Attach the item to the popup.

  // Finally, return no error.
  return (ERR_NONE);
}


// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
tERROR MenuMain::addMenuItemHelp(Window *pParent)
{
  WindowManager *pWm;
  Window *pMenu, *pPopup, *pItem;

  // Validate our arguments.
  if (pParent == NULL) { /* TODO: error */ }

  // Get the window manager to work with.
  pWm = GuiCEGUI::getWm();

  // Create the menu item.
  pMenu = pWm->createWindow("TaharezLook/MenuItem");
  pMenu->setText("Help");         // Set the displayed text.
  pParent->addChildWindow(pMenu); // Attach to the parent.

  // Create the menu popup.
  pPopup = pWm->createWindow("TaharezLook/PopupMenu");
  pMenu->addChildWindow(pPopup);  // Attach the popup to the menu.

  // "About" item in the menu.
  pItem = pWm->createWindow("TaharezLook/MenuItem");
  pItem->setText("About");         // Set the displayed text.
  pItem->subscribeEvent("Clicked", Event::Subscriber(&eventFileAbout));
  pPopup->addChildWindow(pItem);  // Attach the item to the popup.

  // Finally, return no error.
  return (ERR_NONE);
}


// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
bool MenuMain::eventFileAbout(const EventArgs&)
{
//    d_sampleApp->setQuitting(true);
//  NewWindowAbout(pRoot);

  WindowAbout::create();

  puts("About");
  return true;
}


// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
bool MenuMain::eventFileQuit(const EventArgs&)
{
//    d_sampleApp->setQuitting(true);
  puts("Quit");
  return true;
}

